<template>
	<el-container>
		<el-header class="mine-el-header">
			<div class="panel-container">
				<div class="left-panel">

					<el-button
						type="danger"
						plain
						row-key="id"
						icon="el-icon-delete"
						v-auth="['system:apiLog:delete']"
						:disabled="selection.length==0"
						@click="batchDel"
					>删除
					</el-button>
					<el-button
						type="success"
						plain
					>单击行查看日志详情
					</el-button>

				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="queryParams.api_name" clearable placeholder="请输入接口名称"></el-input>

						<el-tooltip class="item" effect="dark" content="搜索" placement="top">
							<el-button type="primary" icon="el-icon-search" @click="handlerSearch"></el-button>
						</el-tooltip>

						<el-tooltip class="item" effect="dark" content="清空条件" placement="top">
							<el-button icon="el-icon-refresh" @click="resetSearch"></el-button>
						</el-tooltip>

						<el-button type="text" @click="toggleFilterPanel">
							{{ povpoerShow ? '关闭更多筛选' : '显示更多筛选' }}
							<el-icon>
								<el-icon-arrow-down v-if="povpoerShow"/>
								<el-icon-arrow-up v-else/>
							</el-icon>
						</el-button>

					</div>
				</div>
			</div>
			<el-card class="filter-panel" shadow="never">
				<el-form label-width="80px" :inline="true">
					<el-form-item label="访问名称" prop="access_name">
						<el-input v-model="queryParams.access_name" clearable placeholder="请输入访问名称"></el-input>
					</el-form-item>

					<el-form-item label="访问时间">
						<el-date-picker
							clearable
							v-model="dateRange"
							type="daterange"
							range-separator="至"
							@change="handleDateChange"
							value-format="YYYY-MM-DD"
							start-placeholder="开始日期"
							end-placeholder="结束日期"
						></el-date-picker>
					</el-form-item>
				</el-form>
			</el-card>
		</el-header>
		<el-main class="nopadding">
			<maTable
				ref="table"
				:api="api"
				@selection-change="selectionChange"
				@row-click="rowClick"
				:params="{orderBy: 'access_time', orderType: 'desc'}"
				stripe
				remoteSort
				remoteFilter
				highlightCurrentRow
			>
				<el-table-column type="selection" width="50"></el-table-column>

<!--				<el-table-column prop="api_name" label="接口名称"></el-table-column>-->

				<el-table-column prop="access_name" width="350" label="接口访问地址" :show-overflow-tooltip="true"></el-table-column>

				<el-table-column prop="ip" label="IP"></el-table-column>

				<el-table-column prop="ip_location" label="访问地点"></el-table-column>

				<el-table-column prop="response_code" label="状态码" width="100">
					<template #default="scope">
						<el-tag class="ml-2" v-if="scope.row.response_code == 200" type="success">{{scope.row.response_code}}</el-tag>
						<el-tag class="ml-2" v-if="scope.row.response_code == 500" type="danger">{{scope.row.response_code}}</el-tag>
					</template>
				</el-table-column>

				<el-table-column prop="access_time" label="访问时间" width="160"></el-table-column>

			</maTable>
		</el-main>

		<el-drawer v-model="infoDrawer" title="操作日志详情" :size="1200" destroy-on-close>
			<info ref="info"></info>
		</el-drawer>

	</el-container>

</template>

<script>
import info from './info'

export default {
	name: 'system:apiLog',

	components: {
		info
	},

	data() {
		return {
			povpoerShow: false,
			dateRange: '',
			infoDrawer: false,
			api: {
				list: this.$API.apiLog.getPageList
			},
			selection: [],
			queryParams: {
				api_name: undefined,
				access_name: undefined,
				maxDate: undefined,
				minDate: undefined,
				orderBy: 'access_time',
				orderType: 'desc',
			}
		}
	},
	methods: {

		rowClick(row) {
			this.infoDrawer = true
			this.$nextTick(() => {
				this.$refs.info.setData(row)
			})
		},

		//批量删除
		async batchDel() {
			await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				const loading = this.$loading();
				let ids = []
				this.selection.map(item => ids.push(item.id))
				this.$API.apiLog.deletes(ids.join(',')).then(() => {
					this.$refs.table.upData(this.queryParams)
				})
				loading.close()
				this.$message.success("操作成功")
			})
		},

		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},

		// 选择时间事件
		handleDateChange(values) {
			if (values !== null) {
				this.queryParams.minDate = values[0]
				this.queryParams.maxDate = values[1]
			}
		},

		toggleFilterPanel() {
			this.povpoerShow = !this.povpoerShow
			document.querySelector('.filter-panel').style.display = this.povpoerShow ? 'block' : 'none'
		},

		//搜索
		handlerSearch() {
			this.$refs.table.upData(this.queryParams)
		},

		resetSearch() {
			this.queryParams = {
				api_name: undefined,
				access_name: undefined,
				maxDate: undefined,
				minDate: undefined,
				orderBy: 'access_time',
				orderType: 'desc',
			}
			this.$refs.table.upData(this.queryParams)
		}
	}
}
</script>

<style>
</style>
